Introduction

Previously, you recapped the process of fetching JSON data from a remote server, converting it to a format that works for Swift, and then storing it in a Core Data database. In this exercise, you’ll practice applying these skills to prepare the data for use in a mobile application.

Scenario

For the food-ordering functionality of the Little Lemon app to work, it will need to display a list of the restaurant’s offerings. This data is readily available on Little Lemon’s web servers, but you’ll need to take a few steps to make this data usable within the app.

Now that your pages are set up you need to connect to the API to fetch the menu items. Below is a diagram that display how your app needs to connect to the API via HTTP as part of the application flow:

Instructions

Continue in the same project as you did in the last exercises and in the previous lesson. The following zip contains two Swift files that you have to add to your project to use the Core Data easier.

The above zip file contains two files: Persistence.swift and FetchedObjects.swift . Add them both to your project files tree by dragging and dropping them in. Make sure the checkbox "Copy items if needed" is selected at the top of the following dialog.

The Persistence file contains a simple core data setup boilerplate code that sets up a simple database file in memory.

FetchedObjets file contains code that makes it easier to fetch the models from the database. As it was explained in the last course and is not the focus of this exercise, you don't need to worry about it. Just consider it as it is, or in other words, as a struct that retrieves objects from Core Data.

Use the following link for the menu items list: menu data

Follow the steps below to implement fetching menu items from the server and then store them in the database.

Step 1: Creating menu models

Create the models that are necessary to map the JSON you get from the link above:

Tip: If you have difficulty completing any of the steps above, please review the following of the course "Working with data in iOS":

Step 2: Querying the server

Open the Menu file you created in the previous exercise, and follow the steps below to create a new method to fetch the menu data from the server. The data can be found at this URL .

Tip: If you have difficulty completing any of the steps above please review the following materials of the course "Working with data in iOS":

Step 3: Parsing response into models using JSONDecoder

Use JSONDecoder to parse the response data inside the completion closure of the data task:

Tip: If you have difficulty completing any of the steps above, please review the following of the course "Working with data in iOS":

Step 4: Initialise core data

Follow the list below to initialize Core Data when your application starts:

Step 5: Create a database model file

Step 6: Add a Dish entity

Tip: If you have difficulty completing any of the steps above please review the following materials of the course "Working with data in iOS":

Step 7: Save mapped menu items into the database

Go back to the Menu view file, and update the getMenuData method to convert the list of MenuItem objects into the list of Dish objects and save them to the database.

Tip: If you have difficulty completing any of the steps above please review the following materials of the course "Working with data in iOS":

Step 8: Displaying the list of menu items from the database

Update the List element of the Menu view to display the Dish items that were saved to the database.

Tip: If you have difficulty completing any of the steps above please review the following materials of the course "Working with data in iOS":

Step 9: Run the code

Run the application. Register, and when you see the home screen with the menu tab, the on-appear function should fetch the menu data, decode it with JSONDecoder , save it to the database by converting the menu items to the Dish objects and finally store them in the database. Immediately after that, the FetchedObjects element will notice that and fetch all the Dish objects from the database and display them in the list as text and image .

You should see a list of menu item information that you have entered inside the text and image elements in the last part of step 7.

(Optional) Step 10: Dish details screen

You could wrap the HStack added in step 8 inside the NavigationLink and create a new SwiftUI file to represent the details of the menu item. The new details screen would need a local constant of a dish. Then you would initialize a new instance of the details screen with a dish argument of the closure, and pass it to the destination parameter in the navigation link constructor. Then it's up to you what information and how would you display it inside the dish details screen. When using the application, when the user presses on any dish row, they will be taken to the details screen.

Conclusion

By completing this exercise, you have implemented data models using a Decodable protocol, fetched a menu list from the server address and mapped them with the JSONDecoder to the models. You also have set up the core data database and initialized it at the start of the application. Finally, you have converted the decoded models to the core data entities, stored them inside the database and displayed them in the Menu screen using the List element.